﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WsdShop.IService;
using SqlSugar;
using Infrastructure;
using Infrastructure.Tool;
using Infrastructure.Pub;
using WsdShop.Model;
using WsdShop.Model.ViewModels;

namespace Admin.Controllers
{
    public class AccountController : Controller
    {
        private IUserInfoService _userService;

        public AccountController(IUserInfoService userService)
        {
            _userService = userService;
        }

        public ActionResult Login()
        {
            return View();
        }

        public JsonResult LoginSubmit(string userName, string passWord)
        {
            Result result = new Result();
            try
            {
                if (userName.IsNullOrEmpty())
                {
                    result.msg = "请输入用户名";
                }
                else if (passWord.IsNullOrEmpty())
                {
                    result.msg = "请输入密码";
                }
                else
                {
                    var user = _userService.GetUserInfo(userName);
                    if (user != null)
                    {
                        if (user.UserState == 1)
                        {
                            result.msg = "该用户已被冻结";
                        }
                        else
                        {
                            if (user.Password == Generator.MD5(passWord))
                            {
                                result.isSuccess = true;
                                Generator.SetCookie(PubConst.CookieUserKey, user.Id.ToString());
                                string uniqueKey = string.Format(PubConst.CacheUserKey, user.Id.ToString());

                                CacheManager<CacheUserInfo>.GetInstance().Add(uniqueKey, user, int.MaxValue);
                                result.msg = "登录成功，正跳转...";
                            }
                            else
                            {
                                result.msg = "用户名密码不正确";
                            }
                        }
                    }
                    else
                    {
                        result.msg = "用户名密码不正确";
                    }
                }
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult GetUserInfo(string userName)
        {
            string uid = Generator.GetCookie(PubConst.CookieUserKey);
            if (uid.IsNullOrEmpty())
            {
                return Json(_userService.GetUserInfo(userName));
            }

            string uniqueKey = string.Format(PubConst.CacheUserKey, uid);
            var user = _userService.GetCacheUser();
            if (user == null || user.Id == 0)
            {
                user = _userService.GetUserInfo(userName);
            }
            return Json(user);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ChangePassword()
        {
            return View();
        }


        [HttpPost]
        public ActionResult GetAppSetting()
        {
            return Json(new { ImgUploadUrl = WsdConfig.GetAppString("ImgUploadUrl"), ImgHostUrl = WsdConfig.GetAppString("ImgHostUrl"), WapHostUrl = WsdConfig.GetAppString("WapHostUrl") });
        }

    }
}